/**
 * Copyright (c) 2010 sonntagsfarben.de
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package de.sonntagsfarben.puremvc.as3.proxy 
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import flash.events.ProgressEvent;
	import flash.utils.Dictionary;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;

	/**
	 * Short description for file.
	 *
	 * @package    de.sonntagsfarben.puremvc.as3.proxy
	 * @author     Soenke Kluth <sonntagsfarben.de> / 2010
	 * @version    SVN: $Id$
	 */
	 
	//TODO muss überarbeitet werden!!!
	//TODO getLoader einführen der einen neuen bulkloader liefert (dictionary) dann eine eigene notification schreiben die während des progress den loader mitübergibt. einen festlegen: preloader! 
	public class ContentProxy extends Proxy
	{
		public static const NAME : String = "ContentProxy";
		
		public static const PRELOADER:String = "PRELOADER";
		
		public static const LOAD_PROGRESS : String = NAME + "LOAD_PROGRESS";
		public static const LOAD_COMPLETE : String = NAME + "LOAD_COMPLETE";
		public static const LOAD_START : String = NAME + "LOAD_START";
		
		private var _bulkloader : BulkLoader;
		private var _completeHandler : Function;
		private var _loaderMap:Dictionary = new Dictionary();
		
		
		public function ContentProxy(name:String = ContentProxy.NAME, data : Object = null)
		{
			super(name, data);
			_initLoader();
		}
		
		
		/****************************************
		*			PRIVATE / PROTECTED
		****************************************/
		
		private function _initLoader() : void
		{
			_bulkloader = new BulkLoader(NAME);
			_bulkloader.addEventListener(BulkLoader.PROGRESS, onProgress);
			_bulkloader.addEventListener(BulkLoader.COMPLETE, onLoadComplete);
		}
		
		
		
		//TODO namen überdenken!!
		protected function addPreloadItems():void
		{
			trace(this+"  addPreloadItems():: override this! to preload!!");		
		}
		
		
		
		/****************************************
		*			EVENT-HANDLER
		****************************************/
		
		
		
		private function onProgress( evt : BulkProgressEvent) : void
		{
			var percent : Number = _bulkloader.weightPercent;
			sendNotification(ContentProxy.LOAD_PROGRESS, percent);
		}

		protected function onLoadComplete(event : ProgressEvent) : void
		{
			if(_completeHandler != null)
				_completeHandler();
				
			try{ 
				//registered yet?
				sendNotification(LOAD_COMPLETE);
			}catch(e:Error){
				trace(this+" "+getProxyName()+" loading is complete but proxy wasn't registered yet");
			}
		}
		

		override public function onRegister() : void
		{
			addPreloadItems();
		}

		override public function onRemove() : void
		{
		}

		
		/****************************************
		*			PUBLIC
		****************************************/
		
		/*
		 * preloading method for AsyncCommand
		 * @param completehandler to call the AsyncCommand complete method
		 */
		public function preloadContent(completeHandler : Function = null) : void
		{
			_completeHandler = completeHandler;
			
			sendNotification(LOAD_START);
			
			if(_bulkloader.itemsTotal > 0)
				_bulkloader.start();	
			else	
				onLoadComplete(null);	
		}
		
		
		protected function get bulkloader() : BulkLoader
		{
			return _bulkloader;
		}		
		
		
		
		public function getLoader(name:String):BulkLoader
		{
			if(_loaderMap[name] != null)
			{
				return _loaderMap[name];
			}
			
			var bulkloader:BulkLoader = new BulkLoader(name);
			bulkloader.addEventListener(BulkLoader.PROGRESS, onProgress);
			bulkloader.addEventListener(BulkLoader.COMPLETE, onLoadComplete);
			
			_loaderMap[name] = bulkloader;
			
			return _loaderMap[name];
		}
		
		
		
			
		
		/****************************************
		*			GETTER-SETTER
		****************************************/
		
		
		public function get loading():Boolean
		{
			return _bulkloader.isRunning;
		}
		
		public function get finished():Boolean
		{
			return bulkloader.isFinished;
		}
		
		
		
		public function get completeHandler() : Function
		{
			return _completeHandler;
		}
		
		
		public function set completeHandler(completeHandler : Function) : void
		{
			_completeHandler = completeHandler;
		}
	}
}